from requests_html import HTMLSession
import pyperclip
import re
import os
import time
'''
https://www.xxbiqudu.com/  笔趣阁
'''

# 获取请求对象
session = HTMLSession()
url = pyperclip.paste()
# 往网站发送get请求
page = session.get(url)
page.encoding = 'utf-8'
list = page.html.find('div#list', first=True)
_name = page.html.find('div#info', first=True)
name = _name.find('h1', first=True).text
print(name)
# 下载地址
path = r'D:\.爬取为txt'
# if(os.path.exists(path) == False):
#     os.mkdir(path)

a_s = list.find('a')
for i,a in enumerate(a_s):
    href = a.attrs['href']
    chap = a.text

    if i > 11:
        print('href ', i, href, chap)
        _page = session.get(href)
        _page.encoding = 'utf-8'
        text = _page.html.find('div#content', first=True).text
        with open(f'{path}\ ' + f'{name}.txt', 'a', encoding='utf-8') as f:
            f.write('\n')
            f.write(chap)
            f.write('\n\n')
            f.write(text)
        time.sleep(0.5)